<template>
	<view>
		<!-- pages/user/profit/index.wxml -->
		<!-- 头部 -->
		<navber id="head-navber" :navbarData="navbarData"></navber>

		<!-- 收益 -->
		<view class="integral">
			<view class="integral-info">
				<image :src="imgUrl + '/imgs/user/profit/bac.png'" class="integral-info-img"></image>
				<div class="integral-info-item">
					<view class="integral-info-title">- 当天收益 -</view>
					<view class="integral-info-number1">{{ dayBean }}</view>
				</div>
				<div class="integral-info-item">
					<view class="integral-info-title">- 总收益 -</view>
					<view class="integral-info-number1">{{ totalBean }}</view>
				</div>
			</view>
			<view class="integral-but">
				<view class="integral-but-item">
					<view class="integral-but-item-text" @tap="switch_check($event, { value: '1' })" data-value="1">
						参与收益
						<view class="integral-but-item-text-bottom" v-if="butCheck == 1"></view>
					</view>
				</view>
				<view class="integral-but-item">
					<view class="integral-but-item-text" @tap="switch_check($event, { value: '2' })" data-value="2">
						推广收益
						<view class="integral-but-item-text-bottom" v-if="butCheck == 2"></view>
					</view>
				</view>
			</view>
			<view class="time-list" v-if="butCheck == 1">
				<view class="time-s">
					<u-datetime-picker :show="show" v-model="timeDay" mode="date" @c @confirm="onConfirm"
						@cancel="onCancel">
					</u-datetime-picker>
					<view @click="show = true">选择：{{timeDayStr}}</view>
				</view>
				<view class="time-all">累计：{{joinBean}}</view>
			</view>
			<view class="time-list2" v-if="butCheck == 2">
				<view class="time-s">
					<u-datetime-picker :show="show" v-model="timeDay" mode="date" @c @confirm="onConfirm"
						@cancel="onCancel">
					</u-datetime-picker>
					<view @click="show = true">选择：{{timeDayStr}}</view>
				</view>
				<view class="time-all">累计：{{popBean}}</view>
			</view>
		</view>
		<!-- 列表 -->
		<view class="integral-list">

			<view class="integral-list-item" v-for="(item, index) in dataList" :key="index">
				<view class="integral-list-item-text1">{{ item.day }}</view>
				<view class="integral-list-item-text2">+{{ item.bean_all }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import navber from '@/components/navber/index';
	// pages/user/profit/index.js
	const Config = require('../../../config/index.js');

	const Api = require('../../../api/index.js');

	export default {
		components: {
			navber
		},
		data() {
			return {
				navbarData: {
					title: '我的收益' //顶部标题
				},
				//导航栏
				imgUrl: Config.imgUrl,
				//图片url
				butCheck: 1,
				dayBean: 0, //当天收益
				totalBean: 0, //我的收益
				joinBean: 0, //参与收益总和
				popBean: 0, //推广收益总和
				dataList: [], //用户收益
				timeDayStr: this.FormatTime(new Date()), //
				timeDay: Number(new Date()), //截止时间（倒退十天记录)
				pagenow: 1, //数据
				show: false
			};
		},
		onReachBottom: function(e) {
			//滚动底部
			this.getData();
		},
		onShow() {
			this.setData({
				dataList: [],
				pagenow: 1
			});
			this.getData();
			this.getTotalData();
		},
		onPullDownRefresh(e) {
			this.setData({
				dataList: [],
				pagenow: 1
			});
			this.getData();
			uni.stopPullDownRefresh();
		},
		methods: {

			onConfirm(e) {
				// console.log(e);
				this.timeDay = e.value;
				// console.log(this.timeDay);
				this.timeDayStr = this.FormatTime(new Date(this.timeDay));
				// console.log(this.timeDayStr);
				this.show = false;
				this.setData({
					dataList: [],
					pagenow: 1
				});
				this.getData();
			},
			onCancel() {
				this.show = false;
			},
			switch_check(e, _dataset) {
				if (!e.currentTarget) {
					if (_dataSet.tagId) {
						e.currentTarget = {
							id: _dataSet.tagId
						};
					} else {
						e.currentTarget = {
							dataset: _dataSet
						};
					}
				}
				//标签
				this.setData({
					butCheck: e.currentTarget.dataset.value,
					pagenow: 1,
					dataList: []
				});
				this.getData();
			},
			getTotalData() {
				//获取总收益
				var json = {};
				Api.getUserStatisticsBean(json).then((res) => {
					if (res.code == 1) {
						this.totalBean = res.data.bean_all;
						this.dayBean = res.data.bean_day;
						this.joinBean = res.data.join_all;
						this.popBean = res.data.pop_all;
					}
				});
			},

			getData() {
				//获取数据
				var json = {
					type: this.butCheck,
					day: this.timeDayStr,
					page: this.pagenow
				};

				Api.getUserTotalBeanPage(json).then((res) => {
					if (res.code == 1) {
						if (res.data) {
							var data = this.dataList;
							data = data.concat(res.data);
							var page = this.pagenow + 1;
							this.setData({
								dataList: data,
								pagenow: page
							});
						}
					}
				});
			},
			FormatTime(dat) {
				//获取年月日，时间
				var year = dat.getFullYear();
				var mon = (dat.getMonth() + 1) < 10 ? "0" + (dat.getMonth() + 1) : dat.getMonth() + 1;
				var data = dat.getDate() < 10 ? "0" + (dat.getDate()) : dat.getDate();
				var newDate = year + "-" + mon + "-" + data
				return newDate;
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>
